function theLength=findLength(P,Q,R)

    controlSequence=[P,Q,R];
    N=3;
    maxLength=norm(P-Q)+norm(Q-R);
    minLength=norm(P-R);
    
    while maxLength-minLength>0.01*minLength %=====1% accuracy
        [controlSequence, N]=subdivision(controlSequence, N);
        maxLength=findMaxLength(controlSequence, N);
        minLength=findMinLength(controlSequence, N);
    end
    
    theLength=(maxLength+minLength)./2;
end

function [newSequence, N]=subdivision(oldSequence, N)

A=zeros(N,(2*N-1));
N=2*N-1;
j=1;
i=1;
while (i<N)
    A(j,i)=1;
    i=i+1;
    A(j:(j+1),i)=[1/2, 1/2];
    i=i+1;
    A(j:(j+2),i)=[1/4, 1/2, 1/4];
    i=i+1; j=j+1;
    A(j:(j+1),i)=[1/2, 1/2];
    i=i+1;
    j=j+1;
end
A(j,i)=1;
newSequence=oldSequence*A;

end

function maxLength=findMaxLength(controlSequence, N)

maxLength=norm(controlSequence(:,1)-controlSequence(:,2));

for i=2:2:N-3
    maxLength=maxLength+norm(controlSequence(:,i)-controlSequence(:,(i+2)));
end

maxLength=maxLength+norm(controlSequence(:,(N-1))-controlSequence(:,N));

end

function minLength=findMinLength(controlSequence, N)

minLength=0;

for i=1:2:N-2
    minLength=minLength+norm(controlSequence(:,i)-controlSequence(:,(i+2)));
end

end